/**
 * FileResourceHyperlink.java
 * created by wesley at 2008-6-7
 */
package net.sf.filelink.hyperlink;

import net.sf.filelink.utl.QueryUtils;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.hyperlink.IHyperlink;

/**
 * FileResourceHyperlink
 * 
 * @author Wesley
 */
public class FileResourceHyperlink implements IHyperlink {

	private final IRegion m_region;

	private final String m_nodeInfo;

	private final IFile m_file;

	/**
	 * constructor for FileResourceHyperlink
	 * 
	 * @param region
	 * @param filePath
	 */
	public FileResourceHyperlink(IRegion region, String nodeInfo, IFile file) {
		m_region = region;
		m_nodeInfo = nodeInfo;
		m_file = file;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.text.hyperlink.IHyperlink#getHyperlinkRegion()
	 */
	public IRegion getHyperlinkRegion() {
		return m_region;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.text.hyperlink.IHyperlink#getHyperlinkText()
	 */
	public String getHyperlinkText() {

		if (m_file != null) {
			return m_file.getProjectRelativePath().toString();
		}

		return "File not found!";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.text.hyperlink.IHyperlink#getTypeLabel()
	 */
	public String getTypeLabel() {
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.text.hyperlink.IHyperlink#open()
	 */
	public void open() {

		if (m_file != null) {
			QueryUtils.openFile(m_file);
		} else {
			QueryUtils.openFileResources(m_nodeInfo);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((m_file == null) ? 0 : m_file.hashCode());
		result = prime * result
				+ ((m_nodeInfo == null) ? 0 : m_nodeInfo.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {

		if (this == obj) {
			return true;
		}

		if (obj == null) {
			return false;
		}

		if (getClass() != obj.getClass()) {
			return false;
		}

		FileResourceHyperlink other = (FileResourceHyperlink) obj;

		if (m_nodeInfo == null) {
			if (other.m_nodeInfo != null) {
				return false;
			}
		} else if (!m_nodeInfo.equals(other.m_nodeInfo)) {
			return false;
		}

		if (m_file == null) {
			if (other.m_file != null) {
				return false;
			}
		} else if (!m_file.equals(other.m_file)) {
			return false;
		}

		return true;
	}

}
